<template>
  <div class="xtx-city" ref="target">
    <div class="select" @click="toggle" :class="{active:isShow}">
      <span v-if='!fullLocation' class="placeholder">请选择配送地址</span>
      <span v-else class="value">{{fullLocation}}</span>
      <i class="iconfont icon-angle-down"></i>
    </div>
    <div class="option" v-show='isShow'>
      <div v-if="loading" class="loading"></div>
      <template v-else>
        <span @click="changeCity(item)" class="ellipsis" v-for="item in cityList" :key="item.code">{{item.name}}</span>
     </template>
    </div>
  </div>
</template>
<script>
import { ref, computed, reactive } from 'vue'
import { onClickOutside } from '@vueuse/core'
import { getCityList } from '@/api/product.js'
export default {
  name: 'XtxCity',
  props: {
    fullLocation: {
      type: String,
      default: ''
    }

  },
  setup (props, { emit }) {
    // 选中的省市区
    const changeResult = reactive({
      provinceCode: '',
      provinceName: '',
      cityCode: '',
      cityName: '',
      countyCode: '',
      countyName: '',
      fullLocation: ''
    })
    // 通过计算属性计算当前显示列表数据,省级:市级 ;县级
    const cityList = computed(() => {
      let result = list.value
      // 当前点击的是省,那么就计算市级列表
      if (changeResult.provinceCode && changeResult.provinceName) {
        // 点击了省，计算它的市级数据
        result = result.find(item => item.code === changeResult.provinceCode).areaList
      }
      // 计算县级列表
      if (changeResult.cityCode && changeResult.cityName) {
        // 点击了省，计算它的市级数据
        return result.find(item => item.code === changeResult.cityCode).areaList
      }
      console.log(result)
      return result
    })
    const changeCity = (city) => {
      if (city.level === 0) {
        // 点击的省级单位
        changeResult.provinceCode = city.code
        changeResult.provinceName = city.name
      } else if (city.level === 1) {
        // 点击的市级单位
        changeResult.cityCode = city.code
        changeResult.cityName = city.name
      } else if (city.level === 2) {
        // 县级
        changeResult.countyCode = city.code
        changeResult.countyName = city.name
        // 全部选择完成前后 传回父组件
        changeResult.fullLocation = `${changeResult.provinceName}${changeResult.cityName}${changeResult.countyName}`
        // 关闭弹窗
        isShow.value = false
        // 选中的数据传递给父组件
        emit('changecity', changeResult)
      }
    }

    const isShow = ref(false)
    const target = ref(null)
    // 城市列表数据
    const list = ref([])
    // 控制选择城市弹窗的显示和隐藏
    // 加载状态
    const loading = ref(true)
    onClickOutside(target, () => {
      isShow.value = false
    })
    const toggle = () => {
      isShow.value = !isShow.value
      if (isShow.value) {
        // 调用接口之前城市数据重置
        for (const key in changeResult) {
          changeResult[key] = ''
        }

        getCityList().then(res => {
          list.value = res
          loading.value = false
        })
      }
    }
    return { isShow, toggle, target, loading, cityList, changeCity }
  }
}
</script>
<style scoped lang="less">
.xtx-city {
  display: inline-block;
  position: relative;
  z-index: 400;
  .select {
    border: 1px solid #e4e4e4;
    height: 30px;
    padding: 0 5px;
    line-height: 28px;
    cursor: pointer;
    &.active {
      background: #fff;
    }
    .placeholder {
      color: #999;
    }
    .value {
      color: #666;
      font-size: 12px;
    }
    i {
      font-size: 12px;
      margin-left: 5px;
    }
  }
  .option {
    width: 542px;
    border: 1px solid #e4e4e4;
    position: absolute;
    left: 0;
    top: 29px;
    background: #fff;
    min-height: 30px;
    line-height: 30px;
    display: flex;
    flex-wrap: wrap;
    padding: 10px;

    .loading {
      height: 290px;
      width: 100%;
      background: url(../../assets/images/loading.gif) no-repeat center;

}
    > span {
      width: 130px;
      text-align: center;
      cursor: pointer;
      border-radius: 4px;
      padding: 0 3px;
      &:hover {
        background: #f5f5f5;
      }
    }
  }
}
</style>
